<template>
	<view>
		<!-- <uni-password ref="secrity" @input="onInput" @confirm="onConfirms" @back="paymentPasswordBack" v-if="pay_pass_display">请输入6位支付密码</uni-password> -->
		<view class="mask" v-show="commentShow"></view>
		<best-payment-password :show="pay_pass_display" digits="6" @submit="onConfirms" @cancel="togglePayment"></best-payment-password>
		<view class="address_box">
			<view class="address_box_name" style="color: #e84b19;">收货人信息</view>
			<view class="address_btn" @click="goaddress">
				选择收货地址
			</view>

			<view class="address_box_name">
				<text style="margin-right: 20upx;">{{ addresslist.consignee }}</text>
				<text>{{ addresslist.phone }}</text>
			</view>
			<view class="address_box_address">
				<text style="color: #e84b19;">详细地址</text>
				<text>{{ addresslist.province }} {{ addresslist.city }} {{ addresslist.district }} {{ addresslist.details }}</text>
			</view>
		</view>
		<!-- 商品 -->
		<view class="content_box">
			<view class="content_box_title"></view>
			<!-- 积分商品 -->
			<view class="content_box_top" v-if="goodsList.property_credit.goods" v-for="(item, index) in goodsList.property_credit.goods"
			 :key="index">
				<view class="content_box_left">
					<image :src="qiniu + item.logo" mode=""></image>
				</view>
				<view class="content_box_right">
					<view class="content_box_right_text">
						{{ item.name }}
						<br />
						{{ item.spu_string }}
					</view>
					<view class="content_box_right_money" v-if="item.property == 1">
						<text style="color:#E84B19;">{{ item.price / 100 }} 积分</text>
						<br />
						<text style="font-size: 24upx; margin-left: 30upx;">x {{ item.number }}</text>
					</view>
					<view class="content_box_right_money" v-else="">
						<text style="color:#E84B19;">{{ item.price / 100 }} 元</text>
						<br />
						<text style="font-size: 24upx; margin-left: 30upx;">x {{ item.number }}</text>
					</view>
				</view>
			</view>
			<!-- 人民币商品 -->
			<view class="content_box_top" v-if="goodsList.property_money.goods" v-for="(item, index) in goodsList.property_money.goods"
			 :key="index">
				<view class="content_box_left">
					<image :src="qiniu + item.image_id" mode=""></image>
				</view>
				<view class="content_box_right">
					<view class="content_box_right_text">
						{{ item.name }}
						<br />
						{{ item.spu_string }}
					</view>
					<view class="content_box_right_money" v-if="item.property == 1">
						<text style="color:#E84B19;">{{ item.price / 100 }} 积分 +￥{{item.additional_money/100}}元</text>
						<br />
						<text style="font-size: 24upx; margin-left: 30upx;">x {{ item.number }}</text>
					</view>
					<view class="content_box_right_money" v-else="">
						<text style="color:#E84B19;">{{ item.price / 100 }} 元 + {{item.additional_credit/100}}积分</text>
						<br />
						<text style="font-size: 24upx; margin-left: 30upx;">x {{ item.number }}</text>
					</view>
				</view>
			</view>
			<view class="content_box_box">
				<view class="content_box_left">配送方式</view>
				<view class="content_box_right">
					<picker class="pickerStyle" :range="expressType" :value="index" @change="pickerType">
						<view class="pickerSelect">
							<text>{{expressType[index]}}</text>
							<view class="icon">
								<uni-icon type="arrowdown" size="22" color='#333'></uni-icon>
							</view>
						</view>

					</picker>
				</view>
			</view>
			<view class="content_box_box" v-if='total_money > 0'>
				<view class="content_box_left">人民币优惠券</view>
				<view class="content_box_right">
					<picker class="pickerStyle" :range="couponMoney" :value="indexMoeny" @change="pickerTypecouponMoney" v-if='couponMoney.length!=0'>
						<view class="pickerSelect">
							<text v-if="couponid==''">请选择优惠券</text>
							<text v-else="">{{couponMoney[indexMoeny]}}</text>
							<view class="icon">
								<uni-icon type="arrowdown" size="22" color='#333'></uni-icon>
							</view>
						</view>
					</picker>
					<view v-else="" style="color: #A3A3A3;">
						暂无优惠券可用
					</view>
				</view>
			</view>
			<view class="content_box_box" v-if="total_credit > 0">
				<view class="content_box_left">积分优惠券</view>
				<view class="content_box_right">
					<picker class="pickerStyle" :range="couponIntegral" :value="index" @change="pickerTypecouponIntegral" v-if='couponIntegral.length!=0'>
						<view class="pickerSelect">
							<text v-if="couponiid==''">请选择优惠券</text>
							<text>{{couponIntegral[index]}}</text>
							<view class="icon">
								<uni-icon type="arrowdown" size="22" color='#333'></uni-icon>
							</view>
						</view>
					</picker>
					<view v-else="" style="color: #A3A3A3;">
						暂无优惠券可用
					</view>
				</view>
			</view>
			<view class="content_box_box">
				<view class="content_box_left">快递费用</view>
				<view class="content_box_right">
					<text v-if='goodsList.shipping.price==0'>免运费</text>
					<text v-else="">
						<text v-if='goodsList.shipping.property==1'>{{goodsList.shipping.price/100}}积分</text>
						<text v-else=''>¥{{goodsList.shipping.price/100}}元</text>
					</text>
				</view>
			</view>
			<view class="content_box_box">
				<view class="content_box_left">订单备注</view>
				<view class="content_box_right"><input type="text" value="" v-model="comment" placeholder="备注信息" /></view>
			</view>
		</view>
		<view class="bootom_box" v-show="!display_pay">
			<view class="bootom_box_text">合计 <text style="color: #E84B19; margin-right:20upx;">{{total_credit/100 }}积分 </text>
				<text style="color: #E84B19;">{{total_money/100}}元</text> </view>
			<view class="bootom_box_btton" @click="submit">提交订单</view>
		</view>

		<!-- 支付 -->
		<view class="pay_box" v-show="display_pay">
			<radio-group @change="changepay">
				<view class="ali-pay" v-if='order_money > 0' >
					<image src="http://rs.eonfox.cc/clzy/static/unknown.png" mode=""></image>
					<text class="ali">余额支付 (剩余:{{balance/100}})</text>
					<label>
						<radio value="1" /></label>
				</view>
				<!-- #ifdef APP-PLUS -->
				<view class="ali-pay" v-if='order_money > 0'>
					<image src="http://rs.eonfox.cc/clzy/static/ali_pay.png" mode=""></image>
					<text class="ali">支付宝</text>
					<label>
						<radio value="2" /></label>
				</view>
				<!-- #endif -->
				<view class="ali-pay" v-if='order_money > 0'>
					<image src="http://rs.eonfox.cc/clzy/static/wechat.png" mode=""></image>
					<text>微信支付</text>
					<label>
						<radio value="3" /></label>
				</view>
				<view class="ali-pay" v-if='order_credit > 0 '>
					<image src="../../static/jifen_ico.png" mode=""></image>
					<text>积分支付</text>
					<label>
						<radio value="4" /></label>
				</view>
				<view class="ali-pay" v-if='order_credit > 0 '>
					<image src="../../static/qianbao_ico.png" mode=""></image>
					<text>积分加麦豆支付</text>
					<label>
						<radio value="5" /></label>
				</view>
			</radio-group>
			<view class="paybtton" @click="pay">支付</view>
		</view>
	</view>
</template>

<script>
	import uniPassword from '@/components/ku3gitxdx-payment-password/ku3gitxdx-payment-password.vue';
	import eonfox from '@/components/eonfox/eonfox.js';
	import fns from '@/components/eonfox/fns.js';
	import pay from '@/components/eonfox/pay.js';
	import uniIcon from "@/components/uni-icon/uni-icon.vue";
	import bestPaymentPassword from '@/components/best-payment-password/best-payment-password.vue'
	var ef = new eonfox();
	export default {
		data() {
			return {
				cart_values: [], //购物车id
				checked_values: [], //规格id
				discountId: '', //购物券id
				address_id: '',
				goodsList: [],
				addresslist: [],
				qiniu: '',
				comment: '', //备注
				paytype: '', //支付方式
				display_pay: false,
				oorderId: '',
				pay_pass_display: false,
				pay_method: '',
				total_credit: '', //积分
				total_money: '', //钱
				password: '',
				order_credit: '', //所需积分
				order_money: '', //所需人民币
				expressId: '', //快递id,
				code: '',
				index: 0,
				indexMoeny: 0,
				indexIntegral: 0,
				expressType: [], //快递类型
				expressIId: [], //快递id
				couponIntegral: [],
				couponMoney: [],
				couponMoneyID: [],
				couponid: '',
				couponIntegralID: [],
				couponiid: '',
				ios_display: '', //判断苹果是否隐藏
				commentShow: '',
				balance:''  //余额
			};
		},
		components: {
			uniPassword,
			uniIcon,
			bestPaymentPassword
		},
		onLoad(open) {
			console.log("传来的值1", open);
			try {
				uni.removeStorageSync('recommendInfo');
			} catch (e) {
				console.log(e)
			}


			var that = this;
			if (open.cart_values) {
				var cartval = open.cart_values.split(',');
				uni.setStorage({
					key: 'cartval',
					data: cartval
				});
				that.cart_values = cartval;
				that.address_id = open.address_id;
				that.expressload()
			}
			that.expressId = that.expressIId[that.index]
			if (!open.cart_values) {
				uni.getStorage({
					key: 'cartval',
					success: function(res) {
						that.cart_values = res.data
						that.address_id = open.addressID
						that.expressload()
					}
				});

			}
			/*7.29注释
			var cartval = open.cart_values.split(',');
			that.cart_values = cartval;
			that.expressId=open.expressId
			that.discountId = open.discountId;
			console.log(that.discountId)
			*/

			// that.checked_values.push(open.checked_values)

			/*7.29注释
			that.address_id = open.address_id;
			ef.submit({
				request: {
					address: ['USERADDRESSSELFGET'], //地址信息
					config: ['APPLICATIONCONFIG'], //配置信息
					msg: ['SHOPORDERSELFCONFIRM', [{ cart_id: that.cart_values, address_id: that.address_id , shop: [{  shipping_id:that.expressId,credit_coupon_id:that.discountId }] }]] //订单信息
				},
				callback(data) {
					var datalist = fns.checkError(data, ['address', 'msg', 'config'], function(errno, error) {
						fns.err(error);
					});
					console.log('datalist', datalist);
					if(datalist==false){
						uni.switchTab({
							url:'../../pages/index/index'
						})
						return
					}
					//地址
					if (datalist.address) {
						that.addresslist = datalist.address;
					}
					//商品信息
					if (datalist.msg && datalist.msg.shop[0]) {
						that.goodsList = datalist.msg.shop[0];
						console.log('goodsList', that.goodsList);
					}
					//商品价格
					if (datalist.msg && datalist.msg && datalist.msg.statistic) {
						that.total_credit= datalist.msg.statistic.total_credit
						that.total_money=datalist.msg.statistic.total_money
					}
					//七牛云
					if (datalist.config && datalist.config.qiniu_domain) {
						that.qiniu = datalist.config.qiniu_domain;
					}
				},
				error(err) {
					fns.err('err', err, 1);
				}
			});*/
		},
		methods: {
			expressload() {
				var that = this
				ef.submit({
					request: {
						express: ['APPLICATIONSHIPPINGSELFOPTIONS', [{
							module: 'shop_order'}]] ,//快递信息
							Balance: ['USERMONEYSELFTOTAL']
					},
					callback(data) {
						var datalist = fns.checkError(data, ['express','Balance'], function(errno, error) {
							fns.err(error);
						})
						console.log('mo',datalist)
						//快递类型
						var arr = []
						if (datalist.express) {
							arr = datalist.express
						}

						arr.forEach(item => {
							that.expressType.push(item.name)
							that.expressIId.push(item.id)
						})
						that.expressId = that.expressIId[0]
						if(datalist.Balance){
							that.balance=datalist.Balance
						}
						
						that.load()
					}
				})
			},
			load() {
				var that = this;
				ef.submit({
					request: {
						config: ['APPLICATIONCONFIG'], //配置信息
						msg: ['SHOPORDERSELFCONFIRM', [{
							cart_id: that.cart_values,
							address_id: that.address_id,
							shop: [{
								shipping_id: that.expressId
							}]
						}]], //订单信息
						address: ['USERADDRESSSELFGET', [{
							id: that.address_id
						}]], //地址信息
					},
					callback(data) {
						var datalist = fns.checkError(data, ['msg', 'config'], function(errno, error) {
							fns.err(error);
						});
						var addressdata = fns.checkError(data, ['address'], function(errno, error) {
							console.log('没有地址')
						});
						//地址
						if (addressdata.address) {
							that.addresslist = addressdata.address;
						}
						console.log('datalist', datalist);

						//商品信息
						if (datalist.msg && datalist.msg.shop[0]) {
							that.goodsList = datalist.msg.shop[0];
							console.log('goodsList', that.goodsList);
						}
						//商品价格
						if (datalist.msg && datalist.msg && datalist.msg.statistic) {
							that.total_credit = datalist.msg.statistic.total_credit
							that.total_money = datalist.msg.statistic.total_money
						}
						if (that.total_credit > 0 && that.total_money > 0) {
							that.getTicketMoney()
							that.getTicketIntegral()
						}
						if (that.total_credit > 0 && that.total_money == 0) {
							that.getTicketIntegral()
						}
						if (that.total_credit == 0 && that.total_money > 0) {
							that.getTicketMoney()
						}

						//七牛云
						if (datalist.config && datalist.config.qiniu_domain) {
							that.qiniu = datalist.config.qiniu_domain;
						}
						//判断ios 是否隐藏
						if (datalist.config && datalist.config.display) {
							that.ios_display = datalist.config.display
						}

					},
					error(err) {
						fns.err('err', err, 1);
					}
				});
			},
			getmoney() {
				var that = this
				ef.submit({
					request: {
						msg: ['SHOPORDERSELFCONFIRM', [{
							cart_id: that.cart_values,
							address_id: that.address_id,
							shop: [{
								shipping_id: that.expressId,
								money_coupon_id: that.couponid,
								credit_coupon_id: that.couponiid
							}]
						}]], //订单信息
					},
					callback(data) {
						var datalist = fns.checkError(data, ['msg'], function(errno, error) {
							fns.err(error);
						})
						if (datalist.msg && datalist.msg && datalist.msg.statistic) {
							that.total_credit = datalist.msg.statistic.total_credit
							that.total_money = datalist.msg.statistic.total_money
						}
						console.log('aaaa', datalist)
					}
				})
			},
			getTicketMoney() {
				//USERCOUPONSELFAVAILABLELIST
				//"(money)[int][二选一][-][金额]": "要获取可用优惠券的订单人民币金额",
				//"(credit)[int][二选一][-][积分数量]": "要获取可用优惠券的订单积分"
				var that = this
				ef.submit({
					request: {
						ticket: ['USERCOUPONSELFAVAILABLELIST', [{
							money: that.total_money
						}]] //快递信息
					},
					callback(data) {
						var datalist = fns.checkError(data, ['ticket'], function(errno, error) {
							fns.err(error);
						})
						console.log('优惠券信息moeny', datalist)
						var arr = []
						if (datalist.ticket) {
							arr = datalist.ticket
						}
						arr.forEach(item => {

							if (item.type == 1) {
								that.couponMoney.push('【' + item.discount + '元优惠券】' + item.min + '~' + item.max + '使用' + '[' + item.name +
									']')
							}
							if (item.type == 2) {
								that.couponMoney.push('【' + item.discount + '代金券】' + item.min + '~' + item.max + '使用' + '[' + item.name +
									']')
							}
							if (item.type == 3) {
								that.couponMoney.push('【' + item.discount + '元优惠券】' + item.min + '~' + item.max + '使用' + '[' + item.name +
									']')
							}
							if (item.type == 4) {
								that.couponMoney.push('【' + item.discount + '折】' + item.min + '~' + item.max + '使用' + '[' + item.name + ']')
							}
							that.couponMoneyID.push(item.id)
						})
						console.log('couponMoney', that.couponMoney)
						console.log('id', that.couponMoneyID)
					}
				})
			},
			getTicketIntegral() {
				var that = this
				ef.submit({
					request: {
						ticket: ['USERCOUPONSELFAVAILABLELIST', [{
							credit: that.total_credit
						}]] //快递信息
					},
					callback(data) {
						var datalist = fns.checkError(data, ['ticket'], function(errno, error) {
							fns.err(error);
						})
						console.log('优惠券信息ticket', datalist)

						var arr = []
						if (datalist.ticket) {
							arr = datalist.ticket
						}
						arr.forEach(item => {
							if (item.type == 1) {
								// that.couponIntegral.push(item.name + item.min + '~' + item.max + '使用,减' + item.discount + '积分')
								that.couponIntegral.push('【' + item.discount + '积分优惠券】' + item.min + '~' + item.max + '使用' + '[' + item.name +
									']')
							}
							if (item.type == 2) {
								that.couponIntegral.push('【' + item.discount + '积分优惠券】' + item.min + '~' + item.max + '使用' + '[' + item.name +
									']')
							}
							if (item.type == 3) {
								that.couponIntegral.push('【' + item.discount + '积分优惠券】' + item.min + '~' + item.max + '使用' + '[' + item.name +
									']')
							}
							if (item.type == 4) {
								that.couponIntegral.push('【' + item.discount + '折】' + item.min + '~' + item.max + '使用' + '[' + item.name +
									']')
							}

							that.couponIntegralID.push(item.id)
						})
						console.log('couponIntegral', that.couponIntegral)
						console.log('id', that.couponIntegralID)
					}
				})
			},
			pickerType(e) {
				console.log('picker发送选择改变，携带值为', e.target.value)
				this.index = e.target.value
				this.expressId = this.expressIId[this.index]
				this.load()
			},
			pickerTypecouponMoney(e) {
				console.log('picker发送选择改变，携带值为', e.target.value)
				this.indexMoeny = e.target.value
				this.couponid = this.couponMoneyID[this.indexMoeny]
				console.log('购物券id', this.couponid)
				this.getmoney()
			},
			pickerTypecouponIntegral(e) {
				this.indexIntegral = e.target.value
				this.couponiid = this.couponIntegralID[this.indexIntegral]
				console.log('购物券id', this.couponiid)
				this.getmoney()
			},
			submit() {
				var that = this;
				ef.submit({
					request: {
						msg: ['SHOPORDERSELFFOUND', [{
							cart_id: that.cart_values,
							address_id: that.address_id,
							shop: [{
								comment: that.comment,
								shipping_id: that.expressId,
								money_coupon_id: that.couponid,
								credit_coupon_id: that.couponiid
							}]
						}]] //订单信息
					},
					callback(data) {
						var datalist = fns.checkError(data, ['msg'], function(errno, error) {
							fns.err(error);
						});
						if (datalist.msg && datalist.msg.order_id) {
							that.oorderId = datalist.msg.order_id;
							that.order_credit = datalist.msg.order_credit; // 提交订单后所需积分
							that.order_money = datalist.msg.order_money; //提交订单后所需人民币
							that.display_pay = true;
							that.commentShow = true
						}
						console.log('display_pay', that.display_pay)
						console.log('datalist', datalist);
						if (that.order_credit > 0 && that.order_money > 0) {
							uni.showToast({
								title: '请先积分支付',
								icon: 'none',
							})
						}
					},
					error(err) {
						fns.err('err', err, 1);
					}
				});
			},
			// 选择支付方式
			changepay(e) {
				this.paytype = e.target.value;
				console.log('paytype', this.paytype);
			},
			//选择地址
			goaddress() {
				var type = "1"
				uni.navigateTo({
					url: '../address/address?type=' + type
				})
			},
			pay() {
				//预付款 支付宝 微信 积分
				var _this = this;
				if (_this.paytype == '') {
					uni.showToast({
						title: '请选择支付方式',
						icon: 'none'
					})
					return
				} else {
					_this.display_pay = false;
					var shopOrderSelfAddConfig = {
						order_id: _this.oorderId
					};

					if (_this.paytype == 1 ) {
						//预付款
						if(_this.balance==0){
							uni.showToast({
								title: '余额不足请充值',
								icon: 'none'
							})
							return
						}
						if (_this.total_credit > 0) {
							uni.showToast({
								title: '请先积分支付',
								icon: 'none'
							})
							_this.display_pay = true;
							return
						} else {
							_this.pay_method = 'user_money';
							_this.pay_pass_display = true;
							shopOrderSelfAddConfig.pay_method = 'user_money';
							_this.pay_money(shopOrderSelfAddConfig);
						}

					}
					if (_this.paytype == 2) {
						//支付宝
						if (_this.total_credit > 0) {
							uni.showToast({
								title: '请先积分支付',
								icon: 'none'
							})
							_this.display_pay = true;
							return
						} else {
							_this.pay_pass_display = false;
							_this.pay_method = 'alipay';
							shopOrderSelfAddConfig.pay_method = 'alipay'; //支付方式 weixinpay 微信支付、alipay 支付宝支付
							shopOrderSelfAddConfig.alipay_trade_type = 'APP';
							_this.shopOrderPayment(shopOrderSelfAddConfig, function(response) {
								_this.pay_alipay(response);
							});
						}
					}
					if (_this.paytype == 3) {
						//微信
						if (_this.total_credit > 0) {
							uni.showToast({
								title: '请先积分支付',
								icon: 'none'
							})
							_this.display_pay = true;
							return
						} else {
							_this.pay_method = 'weixinpay';
							shopOrderSelfAddConfig.pay_method = 'weixinpay';
							// #ifdef MP-WEIXIN
							shopOrderSelfAddConfig.weixin_trade_type = 'JSAPI';
							wx.login({
								//微信小程序登录获取code
								success(res) {
									console.log('wx.login' + JSON.stringify(res));
									if (res.code) {
										_this.code = res.code;
										shopOrderSelfAddConfig.weixin_login_code = res.code;
										_this.shopOrderPayment(shopOrderSelfAddConfig, function(response) {
											_this.pay_weixin(response);
										});
									} else {
										_this.isDisable = false;
										console.log('登录失败！' + res.errMsg);
										console.log('登录失败！' + JSON.stringify(res));
										uni.showToast({
											title: '登录失败！' + res.errMsg,
											icon: 'none'
										});
									}
								}
							});
							// #endif

							// #ifdef APP-PLUS
							shopOrderSelfAddConfig.weixin_trade_type = 'APP';
							_this.shopOrderPayment(shopOrderSelfAddConfig, function(response) {
								_this.pay_weixin(response);
							});
							// #endif

							//#ifdef H5
							_this.payWeChatPMJSAPI()
							//#endif
						}

					}
					if (_this.paytype == 4) {
						// 积分
						_this.pay_method = 'user_credit';
						_this.pay_pass_display = true;
						shopOrderSelfAddConfig.pay_method = 'user_credit';
						_this.pay_money(shopOrderSelfAddConfig);
					}
					if (_this.paytype == 5) {
						_this.pay_method = 'user_credit';
						_this.pay_pass_display = true;
						shopOrderSelfAddConfig.pay_method = 'user_credit';
						_this.pay_money(shopOrderSelfAddConfig);
					}
				}

			},
			//微信公众号支付
			payWeChatPMJSAPI() {
				var _this = this;
				ef.submit({
					request: {
						s: ['SESSIONWEIXINACCESSTOKEN']
					},
					callback(data) {
						var dataList = fns.checkError(data, 's', function(errno, error) {
							uni.showToast({
								title: '请先确认微信授权',
								icon: 'none'
							})
						})
						console.log(dataList);
						if (dataList.s) {
							ef.submit({
								request: {
									s: ['SHOPORDERSELFPAYMENT',
										[{
											order_id: _this.oorderId,
											pay_method: 'weixinpay',
											pay_password: _this.password,
											weixin_login_openid: dataList.s.openid,
											weixin_trade_type: 'MPJSAPI'
										}]
									]
								},

								callback(data) {
									console.log('调起微信支付', data);
									var dataList = fns.checkError(data, 's', function(errno, error) {
										uni.showToast({
											title: error,
											icon: 'none'
										});
									});
									var ress = dataList.s;
									if (ress) {
										console.log('ress', ress);

										var getBrandWCPayRequest = {
											appId: ress.appid,
											timeStamp: String(ress.time_stamp), // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
											nonceStr: ress.nonce_str, // 支付签名随机串，不长于 32 位
											package: 'prepay_id=' + ress.prepay_id, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
											signType: ress.sign_type, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
											paySign: ress.pay_sign, // 支付签名
										};

										console.log('getBrandWCPayRequest', getBrandWCPayRequest);

										function onBridgeReady() {
											WeixinJSBridge.invoke(
												'getBrandWCPayRequest', getBrandWCPayRequest,
												function(res) {
													// uni.showToast({
													// 	title:JSON.stringify(res),
													// 	icon:'none',
													// 	duration:10000
													// })
													if (res.err_msg == "get_brand_wcpay_request:ok") {
														uni.showToast({
															title: '支付成功',
															success() {
																setTimeout(function() {
																	uni.navigateTo({
																		url: '../../pagesB/my-order/my-order'
																	});
																}, 2000)
															}
														})
													}
													if (res.err_msg == "get_brand_wcpay_request:fail") {
														uni.showToast({
															title: '支付失败',
															icon: 'none'
														})
													}
													if (res.err_msg == "get_brand_wcpay_request:cancel") {
														uni.showToast({
															title: '已取消支付',
															icon: 'none',
															success() {
																setTimeout(function() {
																	uni.navigateBack({
																		delta: 1
																	});
																}, 1500)
															}
														})
													}
												});
										}

										if (typeof WeixinJSBridge == "undefined") {
											if (document.addEventListener) {
												document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
											} else if (document.attachEvent) {
												document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
												document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
											}
										} else {
											onBridgeReady();
										}

									} else {
										console.log('提交订单失败', ress);
										_this.payWeChatPMJSAPI();

									}
									console.log('提交订单', ress);

								},
								error(err) {
									console.log('提交订单失败', err, 1);
								}
							});


						} else {

							console.log('location', location.href);

							//当 ACCESSTOKEN 不存在
							ef.left_token(function(left_token) {
								var notify_url = encodeURIComponent(location.href);
								var url = ef.api_server_url + "?" + encodeURI('data=[["SESSIONWEIXINAUTHORIZE",[{"notify_url":"' +
									notify_url + '"}]]]') + "&token=" + left_token;
								console.log(url);
								location.href = url;
							});

						}
					},
					error(err) {
						fns.err('err', err, 1)
					}
				})
			},
			//付款
			shopOrderPayment(shopOrderSelfAddConfig, callbackFunction) {
				var _this = this;
				//如果不是函数，则默认空函数
				if (typeof callbackFunction != 'function') {
					callbackFunction = function() {};
				}

				ef.submit({
					request: {
						s: ['SHOPORDERSELFPAYMENT', [shopOrderSelfAddConfig]]
					},
					callback(data) {
						uni.hideLoading();
						console.log('调起支付', data);
						var shopOrderSelfAddData = fns.checkError(data, 's', function(errno, error) {
							uni.showToast({
								title: error,
								icon: 'none',
								duration: 3000
							});
						});
						if (!shopOrderSelfAddData) {
							return false;
						}
						_this.order_id = shopOrderSelfAddData.s.order_id;
						console.log('提交订单', shopOrderSelfAddData);
						return callbackFunction(shopOrderSelfAddData.s);
					},
					error(err) {
						fns.err('提交订单失败', err, 1);
					}
				});
			},
			//调起预付款支付
			pay_money(response) {
				var _this = this;
				console.log('pay_money()', response);
				// uni.showToast({
				// 	title: '请输入支付密码',
				// 	icon: 'none'
				// });
			},
			togglePayment() {
				this.pay_pass_display = false
				uni.showToast({
					title: '已取消支付',
					icon: 'none',
					success() {
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							});
						}, 1500)
					}
				})

			},
			onConfirms(pwd) {
				var _this = this;
				_this.password = pwd
				console.log('paytype', _this.paytype)
				// console.log('password', password, password.length);

				// 判断是否积分还是余额支付
				if (_this.paytype == 1) {
					_this.paymoeny()
				}
				if (_this.paytype == 4) {
					_this.payjifen()
				}
				if (_this.paytype == 5) {
					_this.payjifen()
				}


				// this.pay_pass_display=false
			},
			//余额支付
			paymoeny() {
				var _this = this
				ef.submit({
					request: {
						s: ['SHOPORDERSELFPAYMENT', [{
							order_id: _this.oorderId,
							pay_method: 'user_money',
							pay_password: _this.password
						}]]
					},
					callback(data) {
						const dataList = fns.checkError(data, 's', function(errno, error) {
							uni.showToast({
								title: error,
								icon: 'none'
							});
						})
						console.log('da...', dataList)

						if (dataList.s.order_id) {
							_this.pay_pass_display = false;
							_this.total_money = ''
							if (_this.total_credit > 0) {
								uni.showToast({
									title: '积分未支付',
									icon: 'none'
								})
							} else {
								console.log('支付成功,积分支付')
								uni.showToast({
									title: '支付成功',
								}, 1500)
								// return
								if (dataList.s.is_new_member == true) {
									uni.showModal({
										title: "提示",
										content: "恭喜您获得购物券，是否前往查看？",
										success(res) {
											if (res.confirm) {
												uni.reLaunch({
													url: '../../pages/me/me',
													success() {
														uni.navigateTo({
															url: '../../pagesB/coupon/coupon',
															success() {
																uni.navigateBack({
																	delta: 1
																})
															}
														})
													}

												})
												return
											} else if (res.cancel) { //点击取消
												uni.reLaunch({
													url: '../../pages/me/me',
													success() {
														uni.navigateTo({
															url: '../../pagesB/my-order/my-order',
															success() {
																uni.navigateBack({
																	delta: 1
																})
															}
														})
													}

												})
												// uni.reLaunch({
												// 		url:
												// 				'../../pages/me/me'
												// 		});	
												// }
												return
											}

										}
									})
								} else {
									setTimeout(function() {
										uni.redirectTo({
											url: '../../pagesB/my-order/my-order'
										});
									}, 1500)
								}

							}

						}

						console.log('支付结果');
					}
				})

			},
			payjifen() {
				var _this = this
				ef.submit({
					request: {
						s: ['SHOPORDERSELFPAYMENT', [{
							order_id: _this.oorderId,
							pay_method: 'user_credit',
							pay_password: _this.password
						}]]
					},
					callback(data) {
						const dataList = fns.checkError(data, 's', function(errno, error) {
							uni.showToast({
								title: error,
								icon: 'none'
							});
						})
						console.log('da...', dataList)
						if (dataList.s.order_id) {
							_this.display_pay = true
							_this.pay_pass_display = false;
							_this.total_credit = ''
							_this.order_credit = 0
							if (_this.paytype == 5) {
								_this.paymoeny()
							} else {
								if (_this.total_money > 0) {
									uni.showToast({
										title: '您还有资金未支付',
										icon: 'none'
									})
								} else {
									uni.showToast({
										title: '支付成功'
									})
									setTimeout(function() {
										uni.redirectTo({
											url: '../../pagesB/my-order/my-order'
										});
									}, 2500);
								}
							}


						}

						console.log('支付结果');
					}
				})

			},
			//微信支付
			pay_weixin(wxArr) {
				var _this = this;
				// #ifdef APP-PLUS
				var orderInfo = {
					appid: wxArr.appid,
					partnerid: wxArr.mch_id, //商户号
					prepayid: wxArr.prepay_id, //预支付交易会话ID
					package: 'Sign=WXPay', //扩展字段,暂填写固定值Sign=WXPay
					noncestr: wxArr.nonce_str, //随机字符串
					timestamp: wxArr.time_stamp, //时间戳
					sign: wxArr.pay_sign //签名
				};
				// #endif
				uni.requestPayment({
					// #ifdef MP-WEIXIN
					provider: 'wxpay',
					timeStamp: String(wxArr.time_stamp),
					nonceStr: wxArr.nonce_str,
					package: 'prepay_id=' + wxArr.prepay_id,
					signType: wxArr.sign_type,
					paySign: wxArr.pay_sign,
					orderInfo: wxArr,
					// #endif
					// #ifdef APP-PLUS
					provider: 'wxpay',
					orderInfo: JSON.stringify(orderInfo),
					// #endif
					success: function(res) {
						_this.test = '支付成功?';
						console.log('支付成功success:', JSON.stringify(res));
						// return
						if (res.errMsg == 'requestPayment:ok') {
							uni.showToast({
								title: '支付成功',
								icon: 'none',
								success() {
									_this.payMoneyDispaly = false;
									_this.total_money = ''
									if (_this.total_credit > 0) {
										_this.paytype == 4
										_this.pay_pass_display = true;
										uni.showToast({
											title: '积分未支付',
											icon: 'none'
										})
									} else {
										uni.showToast({
											title: '支付成功'
										})
										setTimeout(function() {
											uni.redirectTo({
												url: '../../pagesB/my-order/my-order'
											});
										}, 2500)

									}
								}
							});
						}
					},
					fail: function(err) {
						_this.test = '调用支付失败' + JSON.stringify(err);
						console.log('调用支付失败' + JSON.stringify(err));
						uni.showToast({
							title: '已取消支付',
							icon: 'none',
							success() {
								setTimeout(function() {
									uni.navigateBack({
										delta: 1
									});
								}, 1500)
							}
						})
						return false;
					}
				});
			},
			//支付宝支付
			pay_alipay(response) {
				var _this = this;
				_this.pay_pass_display = false;
				var ali = response.alipay;
				uni.requestPayment({
					provider: 'alipay',
					orderInfo: ali,
					success: function(res) {
						console.log('success:' + JSON.stringify(res));
						uni.showToast({
							title: '支付成功',
							icon: 'none',
							success() {
								_this.payMoneyDispaly = false;
								_this.total_money = ''
								if (_this.total_credit > 0) {
									_this.paytype == 4
									_this.pay_pass_display = true;
									uni.showToast({
										title: '积分未支付',
										icon: 'none'
									})
								} else {
									setTimeout(function() {
										uni.showToast({
											title: '支付成功'
										})
									}, 2500)
									uni.redirectTo({
										url: '../../pagesB/my-order/my-order'
									});
								}
							}
						});
					},
					fail: function(err) {
						console.log('fail:' + JSON.stringify(err));
						uni.showToast({
							title: '已取消支付',
							icon: 'none',
							success() {
								setTimeout(function() {
									uni.navigateBack({
										delta: 1
									});
								}, 1500)
							}
						})
					}
				});
			}
		}
	};
</script>

<style>
	page {
		background-color: #f1f1f1;
		font-size: 28upx;
		color: #333333;
	}

	.address_box {
		width: 96%;
		margin-left: 2%;
		background-color: #ffffff;
		height: auto;
		padding-bottom: 20upx;
		border-radius: 10upx;
		margin-top: 20upx;
		padding-top: 20upx;
	}

	.address_box_name {
		width: 95%;
		height: 60upx;
		margin-left: 5%;
	}

	.address_box_address {
		width: 95%;
		height: auto;
		margin-left: 5%;
	}

	/* 商品信息 */
	.content_box {
		width: 92%;
		height: auto;
		margin-left: 3%;
		padding-left: 3%;
		margin-bottom: 120upx;
		background-color: #ffffff;
		border-radius: 10upx;
		margin-top: 50upx;
	}

	.content_box_title {
		width: 100%;
		height: 50upx;
		line-height: 50upx;
	}

	.content_box_top {
		width: 100%;
		height: auto;
		/* height: 200upx; */
		display: flex;
		border-bottom: 1upx #E1E1E1 solid;
		margin-bottom: 10upx;
		margin-top: 10upx;
	}

	.content_box_left {
		width: 30%;
		height: auto;
	}

	.content_box_left image {
		width: 180upx;
		height: 170upx;
	}

	.content_box_right {
		width: 70%;
		height: auto;
		display: flex;
	}

	.content_box_right_text {
		width: 67%;
	}

	.content_box_right_money {
		width: 30%;
	}

	.content_box_box {
		width: 100%;
		display: flex;
		margin-top: 20upx;
	}

	.bootom_box {
		width: 98%;
		position: fixed;
		bottom: 0upx;
		background-color: #ffffff;
		height: 90upx;
		line-height: 90upx;
		border-radius: 10upx;
		display: flex;
		padding-left: 2%;
	}

	.bootom_box_text {
		width: 70%;
	}

	.bootom_box_btton {
		width: 30%;
		background-color: #e84b19;
		color: #ffffff;
		text-align: center;
		border-radius: 15upx;
	}

	.pay_box {
		width: 100%;
		height: auto;
		position: absolute;
		bottom: 0upx;
		background-color: #ffffff;
		z-index: 20;
	}

	.ali-pay {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: left;
		border-bottom: 1px solid #ccc;
		position: relative;
		height: 80upx;
		justify-content: space-around;
	}

	.ali-pay image {
		width: 24px;
		height: 24px;
	}

	.paybtton {
		width: 100%;
		height: 80upx;
		background-color: #e84b19;
		color: #ffffff;
		text-align: center;
		line-height: 80upx;
	}

	.address_btn {
		width: 200upx;
		height: 60upx;
		line-height: 60upx;
		background-color: #E44545;
		color: #FFFFFF;
		border-radius: 10upx;
		text-align: center;
		position: absolute;
		right: 20upx;
		top: 40upx;
	}

	.icon {
		width: 10%;
		margin-left: 20upx;
	}

	picker {
		width: 100%;
		display: flex;
	}

	.pickerSelect {
		display: flex;
	}

	.mask {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 4;
		width: 100%;
		height: 100vh;
		background: rgba(0, 0, 0, 0.5);
	}
</style>
